<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="generator" content="pdoc 0.10.0" />
<title>netmiko.scp_functions API documentation</title>
<meta name="description" content="Netmiko SCP operations …" />
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/sanitize.min.css" integrity="sha256-PK9q560IAAa6WVRRh76LtCaI8pjTJ2z11v0miyNNjrs=" crossorigin>
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/11.0.1/typography.min.css" integrity="sha256-7l/o7C8jubJiy74VsKTidCy1yBkRtiUGbVkYBylBqUg=" crossorigin>
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/styles/github.min.css" crossorigin>
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.1/highlight.min.js" integrity="sha256-Uv3H6lx7dJmRfRvH8TH6kJD1TSK1aFcwgx+mdg3epi8=" crossorigin></script>
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>netmiko.scp_functions</code></h1>
</header>
<section id="section-intro">
<p>Netmiko SCP operations.</p>
<p>Supports file get and file put operations.</p>
<p>SCP requires a separate SSH connection for a control channel.</p>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">&#34;&#34;&#34;
Netmiko SCP operations.

Supports file get and file put operations.

SCP requires a separate SSH connection for a control channel.
&#34;&#34;&#34;
from typing import AnyStr, Optional, Callable, Any, Dict
from typing import TYPE_CHECKING
from netmiko.scp_handler import BaseFileTransfer
from netmiko.ssh_dispatcher import FileTransfer
from netmiko.cisco.cisco_ios import InLineTransfer

if TYPE_CHECKING:
    from netmiko.base_connection import BaseConnection


def progress_bar(
    filename: AnyStr, size: int, sent: int, peername: Optional[str] = None
) -&gt; None:
    max_width = 50
    if isinstance(filename, bytes):
        filename_str = filename.decode()
    else:
        filename_str = filename
    clear_screen = chr(27) + &#34;[2J&#34;
    terminating_char = &#34;|&#34;

    # Percentage done
    percent_complete = sent / size
    percent_str = f&#34;{percent_complete*100:.2f}%&#34;
    hash_count = int(percent_complete * max_width)
    progress = hash_count * &#34;&gt;&#34;

    if peername is None:
        header_msg = f&#34;Transferring file: {filename_str}\n&#34;
    else:
        header_msg = f&#34;Transferring file to {peername}: {filename_str}\n&#34;

    msg = f&#34;{progress:&lt;50}{terminating_char:1} ({percent_str})&#34;
    print(clear_screen)
    print(header_msg)
    print(msg)


def verifyspace_and_transferfile(scp_transfer: BaseFileTransfer) -&gt; None:
    &#34;&#34;&#34;Verify space and transfer file.&#34;&#34;&#34;
    if not scp_transfer.verify_space_available():
        raise ValueError(&#34;Insufficient space available on remote device&#34;)
    scp_transfer.transfer_file()


def file_transfer(
    ssh_conn: &#34;BaseConnection&#34;,
    source_file: str,
    dest_file: str,
    file_system: Optional[str] = None,
    direction: str = &#34;put&#34;,
    disable_md5: bool = False,
    inline_transfer: bool = False,
    overwrite_file: bool = False,
    socket_timeout: float = 10.0,
    progress: Optional[Callable[..., Any]] = None,
    progress4: Optional[Callable[..., Any]] = None,
    verify_file: Optional[bool] = None,
) -&gt; Dict[str, bool]:
    &#34;&#34;&#34;Use Secure Copy or Inline (IOS-only) to transfer files to/from network devices.

    inline_transfer ONLY SUPPORTS TEXT FILES and will not support binary file transfers.

    return {
        &#39;file_exists&#39;: boolean,
        &#39;file_transferred&#39;: boolean,
        &#39;file_verified&#39;: boolean,
    }
    &#34;&#34;&#34;
    transferred_and_verified = {
        &#34;file_exists&#34;: True,
        &#34;file_transferred&#34;: True,
        &#34;file_verified&#34;: True,
    }
    transferred_and_notverified = {
        &#34;file_exists&#34;: True,
        &#34;file_transferred&#34;: True,
        &#34;file_verified&#34;: False,
    }
    nottransferred_but_verified = {
        &#34;file_exists&#34;: True,
        &#34;file_transferred&#34;: False,
        &#34;file_verified&#34;: True,
    }

    if &#34;cisco_ios&#34; in ssh_conn.device_type or &#34;cisco_xe&#34; in ssh_conn.device_type:
        cisco_ios = True
    else:
        cisco_ios = False
    if not cisco_ios and inline_transfer:
        raise ValueError(&#34;Inline Transfer only supported for Cisco IOS/Cisco IOS-XE&#34;)

    # Replace disable_md5 argument with verify_file argument across time
    if verify_file is None:
        verify_file = not disable_md5

    scp_args = {
        &#34;ssh_conn&#34;: ssh_conn,
        &#34;source_file&#34;: source_file,
        &#34;dest_file&#34;: dest_file,
        &#34;direction&#34;: direction,
        &#34;socket_timeout&#34;: socket_timeout,
        &#34;progress&#34;: progress,
        &#34;progress4&#34;: progress4,
    }
    if file_system is not None:
        scp_args[&#34;file_system&#34;] = file_system

    TransferClass: Callable[..., BaseFileTransfer]
    if inline_transfer:
        TransferClass = InLineTransfer
    else:
        TransferClass = FileTransfer

    with TransferClass(**scp_args) as scp_transfer:
        if scp_transfer.check_file_exists():
            if overwrite_file:
                if verify_file:
                    if scp_transfer.verify_file():
                        return nottransferred_but_verified
                    else:
                        # File exists, you can overwrite it, MD5 is wrong (transfer file)
                        verifyspace_and_transferfile(scp_transfer)
                        if scp_transfer.verify_file():
                            return transferred_and_verified
                        else:
                            raise ValueError(
                                &#34;MD5 failure between source and destination files&#34;
                            )
                else:
                    # File exists, you can overwrite it, but MD5 not allowed (transfer file)
                    verifyspace_and_transferfile(scp_transfer)
                    return transferred_and_notverified
            else:
                # File exists, but you can&#39;t overwrite it.
                if verify_file:
                    if scp_transfer.verify_file():
                        return nottransferred_but_verified
                msg = &#34;File already exists and overwrite_file is disabled&#34;
                raise ValueError(msg)
        else:
            verifyspace_and_transferfile(scp_transfer)
            # File doesn&#39;t exist
            if verify_file:
                if scp_transfer.verify_file():
                    return transferred_and_verified
                else:
                    raise ValueError(&#34;MD5 failure between source and destination files&#34;)
            else:
                return transferred_and_notverified</code></pre>
</details>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-functions">Functions</h2>
<dl>
<dt id="netmiko.scp_functions.file_transfer"><code class="name flex">
<span>def <span class="ident">file_transfer</span></span>(<span>ssh_conn: BaseConnection, source_file: str, dest_file: str, file_system: Optional[str] = None, direction: str = 'put', disable_md5: bool = False, inline_transfer: bool = False, overwrite_file: bool = False, socket_timeout: float = 10.0, progress: Optional[Callable[..., Any]] = None, progress4: Optional[Callable[..., Any]] = None, verify_file: Optional[bool] = None) ‑> Dict[str, bool]</span>
</code></dt>
<dd>
<div class="desc"><p>Use Secure Copy or Inline (IOS-only) to transfer files to/from network devices.</p>
<p>inline_transfer ONLY SUPPORTS TEXT FILES and will not support binary file transfers.</p>
<p>return {
'file_exists': boolean,
'file_transferred': boolean,
'file_verified': boolean,
}</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def file_transfer(
    ssh_conn: &#34;BaseConnection&#34;,
    source_file: str,
    dest_file: str,
    file_system: Optional[str] = None,
    direction: str = &#34;put&#34;,
    disable_md5: bool = False,
    inline_transfer: bool = False,
    overwrite_file: bool = False,
    socket_timeout: float = 10.0,
    progress: Optional[Callable[..., Any]] = None,
    progress4: Optional[Callable[..., Any]] = None,
    verify_file: Optional[bool] = None,
) -&gt; Dict[str, bool]:
    &#34;&#34;&#34;Use Secure Copy or Inline (IOS-only) to transfer files to/from network devices.

    inline_transfer ONLY SUPPORTS TEXT FILES and will not support binary file transfers.

    return {
        &#39;file_exists&#39;: boolean,
        &#39;file_transferred&#39;: boolean,
        &#39;file_verified&#39;: boolean,
    }
    &#34;&#34;&#34;
    transferred_and_verified = {
        &#34;file_exists&#34;: True,
        &#34;file_transferred&#34;: True,
        &#34;file_verified&#34;: True,
    }
    transferred_and_notverified = {
        &#34;file_exists&#34;: True,
        &#34;file_transferred&#34;: True,
        &#34;file_verified&#34;: False,
    }
    nottransferred_but_verified = {
        &#34;file_exists&#34;: True,
        &#34;file_transferred&#34;: False,
        &#34;file_verified&#34;: True,
    }

    if &#34;cisco_ios&#34; in ssh_conn.device_type or &#34;cisco_xe&#34; in ssh_conn.device_type:
        cisco_ios = True
    else:
        cisco_ios = False
    if not cisco_ios and inline_transfer:
        raise ValueError(&#34;Inline Transfer only supported for Cisco IOS/Cisco IOS-XE&#34;)

    # Replace disable_md5 argument with verify_file argument across time
    if verify_file is None:
        verify_file = not disable_md5

    scp_args = {
        &#34;ssh_conn&#34;: ssh_conn,
        &#34;source_file&#34;: source_file,
        &#34;dest_file&#34;: dest_file,
        &#34;direction&#34;: direction,
        &#34;socket_timeout&#34;: socket_timeout,
        &#34;progress&#34;: progress,
        &#34;progress4&#34;: progress4,
    }
    if file_system is not None:
        scp_args[&#34;file_system&#34;] = file_system

    TransferClass: Callable[..., BaseFileTransfer]
    if inline_transfer:
        TransferClass = InLineTransfer
    else:
        TransferClass = FileTransfer

    with TransferClass(**scp_args) as scp_transfer:
        if scp_transfer.check_file_exists():
            if overwrite_file:
                if verify_file:
                    if scp_transfer.verify_file():
                        return nottransferred_but_verified
                    else:
                        # File exists, you can overwrite it, MD5 is wrong (transfer file)
                        verifyspace_and_transferfile(scp_transfer)
                        if scp_transfer.verify_file():
                            return transferred_and_verified
                        else:
                            raise ValueError(
                                &#34;MD5 failure between source and destination files&#34;
                            )
                else:
                    # File exists, you can overwrite it, but MD5 not allowed (transfer file)
                    verifyspace_and_transferfile(scp_transfer)
                    return transferred_and_notverified
            else:
                # File exists, but you can&#39;t overwrite it.
                if verify_file:
                    if scp_transfer.verify_file():
                        return nottransferred_but_verified
                msg = &#34;File already exists and overwrite_file is disabled&#34;
                raise ValueError(msg)
        else:
            verifyspace_and_transferfile(scp_transfer)
            # File doesn&#39;t exist
            if verify_file:
                if scp_transfer.verify_file():
                    return transferred_and_verified
                else:
                    raise ValueError(&#34;MD5 failure between source and destination files&#34;)
            else:
                return transferred_and_notverified</code></pre>
</details>
</dd>
<dt id="netmiko.scp_functions.progress_bar"><code class="name flex">
<span>def <span class="ident">progress_bar</span></span>(<span>filename: ~AnyStr, size: int, sent: int, peername: Optional[str] = None) ‑> None</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def progress_bar(
    filename: AnyStr, size: int, sent: int, peername: Optional[str] = None
) -&gt; None:
    max_width = 50
    if isinstance(filename, bytes):
        filename_str = filename.decode()
    else:
        filename_str = filename
    clear_screen = chr(27) + &#34;[2J&#34;
    terminating_char = &#34;|&#34;

    # Percentage done
    percent_complete = sent / size
    percent_str = f&#34;{percent_complete*100:.2f}%&#34;
    hash_count = int(percent_complete * max_width)
    progress = hash_count * &#34;&gt;&#34;

    if peername is None:
        header_msg = f&#34;Transferring file: {filename_str}\n&#34;
    else:
        header_msg = f&#34;Transferring file to {peername}: {filename_str}\n&#34;

    msg = f&#34;{progress:&lt;50}{terminating_char:1} ({percent_str})&#34;
    print(clear_screen)
    print(header_msg)
    print(msg)</code></pre>
</details>
</dd>
<dt id="netmiko.scp_functions.verifyspace_and_transferfile"><code class="name flex">
<span>def <span class="ident">verifyspace_and_transferfile</span></span>(<span>scp_transfer: <a title="netmiko.scp_handler.BaseFileTransfer" href="scp_handler.html#netmiko.scp_handler.BaseFileTransfer">BaseFileTransfer</a>) ‑> None</span>
</code></dt>
<dd>
<div class="desc"><p>Verify space and transfer file.</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def verifyspace_and_transferfile(scp_transfer: BaseFileTransfer) -&gt; None:
    &#34;&#34;&#34;Verify space and transfer file.&#34;&#34;&#34;
    if not scp_transfer.verify_space_available():
        raise ValueError(&#34;Insufficient space available on remote device&#34;)
    scp_transfer.transfer_file()</code></pre>
</details>
</dd>
</dl>
</section>
<section>
</section>
</article>
<nav id="sidebar">
<h1>Index</h1>
<div class="toc">
<ul></ul>
</div>
<ul id="index">
<li><h3>Super-module</h3>
<ul>
<li><code><a title="netmiko" href="index.html">netmiko</a></code></li>
</ul>
</li>
<li><h3><a href="#header-functions">Functions</a></h3>
<ul class="">
<li><code><a title="netmiko.scp_functions.file_transfer" href="#netmiko.scp_functions.file_transfer">file_transfer</a></code></li>
<li><code><a title="netmiko.scp_functions.progress_bar" href="#netmiko.scp_functions.progress_bar">progress_bar</a></code></li>
<li><code><a title="netmiko.scp_functions.verifyspace_and_transferfile" href="#netmiko.scp_functions.verifyspace_and_transferfile">verifyspace_and_transferfile</a></code></li>
</ul>
</li>
</ul>
</nav>
</main>
<footer id="footer">
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
</footer>
</body>
</html>